package com.baturu.vin.benz.service.impl.supers;

import com.baturu.carvin.service.impl.AppConfigLoader;
import com.baturu.vin.base.constant.MySQLVariables;
import com.baturu.vin.benz.consts.BenzLandEnum;
import com.baturu.vin.benz.dal.dao.BenzImageDAO;
import com.baturu.vin.benz.dal.dao.BenzSubGroupDAO;
import com.baturu.vin.benz.dto.*;
import com.baturu.vin.benz.service.impl.BenzCarTypeInfoService;
import com.baturu.vin.benz.service.impl.util.BenzI18nService;
import com.baturu.vin.dto.EPCAnalyserEnum;
import com.baturu.vin.dto.EPCImagePointDTO;
import com.baturu.vin.kit.StreamKit;
import com.google.common.collect.Lists;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * BenzSuperSubGroupService
 * 奔驰通过主组查询子组信息
 *
 * @author majianming
 * @date 2019-04-19 15:07
 */
@Service
public class BenzSuperSubGroupService {

    @Autowired
    private BenzSubGroupDAO benzSubGroupDAO;

    @Autowired
    private BenzImageDAO imageDAO;

    @Autowired
    private AppConfigLoader appConfigLoader;


    @Autowired
    private BenzCarTypeInfoService benzCarTypeInfoService;

    @Autowired
    private BenzI18nService benzI18nService;


    public List<BenzSuperSubGroupDTO> querySubGroup(BenzSuperCarInfoDTO carInfoDTO, BenzSuperMainGroupDTO mainGroupDTO) {
        String mainGroupNo;
        if (Objects.isNull(carInfoDTO) || Objects.isNull(mainGroupDTO) || StringUtils.isBlank(mainGroupNo = mainGroupDTO.getGroupNo())) {
            return Collections.emptyList();
        }

        String landCode = benzI18nService.getBenzLangEnum().getLandCode();
        String imgBasePath = appConfigLoader.getEPCPartsImageFullUrl(EPCAnalyserEnum.BENZ, "");
        List<BenzSuperSubGroupDTO> benzSuperSubGroupDTOS = benzSubGroupDAO.querySubGroupByMainGroup(landCode, mainGroupDTO.getBaseCode(), mainGroupNo);
        List<BenzImageDTO> imageDTOList = imageDAO.queryImageCode(Lists.newArrayList(mainGroupDTO.getBaseCode()), Lists.newArrayList(mainGroupNo));

//        //查询catnum,groupnum,subgrp下所有的图片以及callout
        List<BenzImageDTO> benzImageDTOs = benzCarTypeInfoService.findByPartsCodeByQueryParam(imageDTOList);


//        List<BenzImgPositionDTO> imgPositionQueryParam = benzImageDTOs.stream()
//                .filter(l -> StringUtils.isNotBlank(l.getCallout()))
//                .flatMap(l ->
//                        l.getCallouts().stream()
//                                .map(
//                                        c -> BenzImgPositionDTO.builder().callout(c).imgName(l.getImageCode()).build()
//                                )
//                ).collect(Collectors.toList());
//
//        Map<String, List<BenzImgPositionDTO>> benzImgPositionDTOMapImageCode = benzCarTypeInfoService.findImgPositionByQueryParam(imgPositionQueryParam).stream().collect(Collectors.groupingBy(BenzImgPositionDTO::getImgName));

        Map<String, List<BenzImageDTO>> imageGroups = benzImageDTOs.stream().collect(Collectors.groupingBy(BenzImageDTO::getSubgrp));

        benzSuperSubGroupDTOS.forEach(l -> {
            l.setBaseCode(mainGroupDTO.getBaseCode());
            String subGroupNo = l.getSubGroupNo();
            List<BenzImageDTO> benzImageDTOS = imageGroups.get(subGroupNo);
            List<BenzImagesDTO> benzImagesDTOS = benzImageDTOS.stream().map(
                    benzImageDTO -> {
//                        List<BenzImgPositionDTO> imgPositionDTOS = benzImgPositionDTOMapImageCode.get(benzImageDTO.getImageCode());
//                        if (CollectionUtils.isEmpty(imgPositionDTOS)) {
//                            return null;
//                        }
//                        List<BenzImagePositionDTO> imagePositionDTOS = imgPositionDTOS.stream().map(postionDTO -> {
//                            EPCImagePointDTO topLeft = new EPCImagePointDTO();
//                            EPCImagePointDTO bottomRight = new EPCImagePointDTO();
//                            topLeft.setX(postionDTO.getTopLeftX());
//                            topLeft.setY(postionDTO.getTopLeftY());
//                            bottomRight.setX(postionDTO.getBottomRightX());
//                            bottomRight.setY(postionDTO.getBottomRightY());
//                            return BenzImagePositionDTO.builder().position(postionDTO.getCallout()).bottomRight(bottomRight).topLeft(topLeft).build();
//                        }).collect(Collectors.toList());
                        String catalog = benzImageDTO.getImgtype() + benzImageDTO.getGroupnum() + benzImageDTO.getSubgrp();
                        String imageUrl = imgBasePath + catalog + "/" + benzImageDTO.getImageCode() + ".png";
                        return BenzImagesDTO.builder().imageUrl(imageUrl)
                                .imageCode(benzImageDTO.getImageCode())
//                                .imagePositions(imagePositionDTOS)
                                .build();
                    }).filter(Objects::nonNull).collect(Collectors.toList());
            l.setBenzImagesDTOS(benzImagesDTOS);
            if (CollectionUtils.isNotEmpty(benzImagesDTOS)) {
                BenzImagesDTO benzImagesDTO = benzImagesDTOS.get(0);
                l.setImagePath(benzImagesDTO.getImageUrl());
            }
        });
        return benzSuperSubGroupDTOS;
    }

    /**
     * 通过子组来获取子组信息
     *
     * @param subGroupDTOS
     * @return
     */
    public List<BenzSuperSubGroupDTO> querySubInfo(List<BenzSuperSubGroupDTO> subGroupDTOS, BenzLandEnum lang) {
        if (CollectionUtils.isEmpty(subGroupDTOS)) {
            return Collections.emptyList();
        }
        BenzSuperSubGroupDTO benzSuperSubGroupDTO = subGroupDTOS.get(0);
        String imgBasePath = appConfigLoader.getEPCPartsImageFullUrl(EPCAnalyserEnum.BENZ, "");
        List<BenzSuperSubGroupDTO> benzSuperSubGroupDTOS = benzSubGroupDAO.querySubGroupByMainGroup(lang.getLandCode(), benzSuperSubGroupDTO.getCatNum(), benzSuperSubGroupDTO.getMainGroupNo());
        List<BenzImageDTO> imageDTOList = imageDAO.queryImageCode(Lists.newArrayList(benzSuperSubGroupDTO.getCatNum()), Lists.newArrayList(benzSuperSubGroupDTO.getMainGroupNo()));

//        //查询catnum,groupnum,subgrp下所有的图片以及callout
        List<BenzImageDTO> benzImageDTOs = benzCarTypeInfoService.findByPartsCodeByQueryParam(imageDTOList);


        List<BenzImgPositionDTO> imgPositionQueryParam = benzImageDTOs.stream()
                .filter(l -> StringUtils.isNotBlank(l.getCallout()))
                .flatMap(l ->
                        l.getCallouts().stream()
                                .map(
                                        c -> BenzImgPositionDTO.builder().callout(c).imgName(l.getImageCode()).build()
                                )
                ).collect(Collectors.toList());
//
        Map<String, List<BenzImgPositionDTO>> benzImgPositionDTOMapImageCode = benzCarTypeInfoService.findImgPositionByQueryParam(imgPositionQueryParam).stream().collect(Collectors.groupingBy(BenzImgPositionDTO::getImgName));

        Map<String, List<BenzImageDTO>> imageGroups = benzImageDTOs.stream().collect(Collectors.groupingBy(BenzImageDTO::getSubgrp));
        List<BenzSuperSubGroupDTO> superSubGroupDTOS = StreamKit.partitionStream(benzSuperSubGroupDTOS, MySQLVariables.EQ_RANGE_INDEX_DIVE_LIMIT).flatMap(
                l -> benzSubGroupDAO.querySubGroupBySubGroup(l, lang.getLandCode()).stream()
        ).collect(Collectors.toList());

        superSubGroupDTOS.forEach(l -> {
            l.setBaseCode(benzSuperSubGroupDTO.getCatNum());
            String subGroupNo = l.getSubGroupNo();
            List<BenzImageDTO> benzImageDTOS = imageGroups.get(subGroupNo);
            List<BenzImagesDTO> benzImagesDTOS = benzImageDTOS.stream().map(
                    benzImageDTO -> {
                        List<BenzImgPositionDTO> imgPositionDTOS = benzImgPositionDTOMapImageCode.get(benzImageDTO.getImageCode());
                        if (CollectionUtils.isEmpty(imgPositionDTOS)) {
                            return null;
                        }
                        List<BenzImagePositionDTO> imagePositionDTOS = imgPositionDTOS.stream().map(postionDTO -> {
                            EPCImagePointDTO topLeft = new EPCImagePointDTO();
                            EPCImagePointDTO bottomRight = new EPCImagePointDTO();
                            topLeft.setX(postionDTO.getTopLeftX());
                            topLeft.setY(postionDTO.getTopLeftY());
                            bottomRight.setX(postionDTO.getBottomRightX());
                            bottomRight.setY(postionDTO.getBottomRightY());
                            return BenzImagePositionDTO.builder().position(postionDTO.getCallout()).bottomRight(bottomRight).topLeft(topLeft).build();
                        }).collect(Collectors.toList());
                        String catalog = benzImageDTO.getImgtype() + benzImageDTO.getGroupnum() + benzImageDTO.getSubgrp();
                        String imageUrl = imgBasePath + catalog + "/" + benzImageDTO.getImageCode() + ".png";
                        return BenzImagesDTO.builder().imageUrl(imageUrl).imagePositions(imagePositionDTOS).build();
                    }).filter(Objects::nonNull).collect(Collectors.toList());
            l.setBenzImagesDTOS(benzImagesDTOS);
        });
        if (CollectionUtils.isEmpty(benzSuperSubGroupDTOS)) {
            return Collections.emptyList();
        }

        superSubGroupDTOS.forEach(l -> {
            l.setBaseCode(l.getCatNum());
            l.setBenzImagesDTOS(benzSuperSubGroupDTO.getBenzImagesDTOS());
        });
        return superSubGroupDTOS;
    }
}